Public Class Parser
#Region "Define"

    Private mUseArmory As Boolean = True
    Private mUpdateSpellDb As Boolean = True

    Public mRealm As String = "Proudmoore"
    Public mRegion As String = "US"
    Private CharaInfo As New WowCrawler.CharactersInfo

    Private GuessClsName As New WowCrawler.SpellAndClassDB

    Public Property Realm() As String
        Get
            Return mRealm
        End Get
        Set(ByVal value As String)
            If value <> "" Then
                mRealm = value
            End If
        End Set
    End Property

    Public Property Region() As String
        Get
            Return mRegion
        End Get
        Set(ByVal value As String)
            Select Case UCase(value)
                Case "US"
                    mRegion = "US"
                Case "EU"
                    mRegion = "EU"
                Case Else
            End Select
        End Set
    End Property

    Public Property UseArmory() As Boolean
        Get
            Return mUseArmory
        End Get
        Set(ByVal value As Boolean)
            mUseArmory = value
            If mUseArmory = False Then
                mUpdateSpellDb = False
            End If
        End Set
    End Property

    Public Property UpdateSpellDb() As Boolean
        Get
            Return mUpdateSpellDb
        End Get
        Set(ByVal value As Boolean)
            mUpdateSpellDb = value
            If mUseArmory = False Then
                mUpdateSpellDb = False
            End If
        End Set
    End Property

    Public Enum ParserMode
        Window
        CommandLine
    End Enum
#End Region

    Private Sub ShowProgress(ByVal Prog As Integer)
        formMain.ToolStripStatusLabel1.Text = CStr(Prog) & "%"
    End Sub

    Public Sub Parse(ByVal LogFilename As String)
        Dim CLF As New CombatLogFile
        CLF.ReadLogFile(LogFilename)

        Dim Ev As New EventDB
        Dim EvData(CLF.CombatLog.Length - 1) As EventDB.EventDataEntry

        Dim Act As New Actors
        Dim actorIdx As Integer

        Dim buffer() As String
        Dim i As Long

        Dim Split As New LogSplitter

        Dim prog As Integer = 0
        For i = 0 To CLF.CombatLog.Length - 1
            buffer = CLF.Items(i)

            EvData(i) = New EventDB.EventDataEntry
            'Ev.InitEventData(EvData)
            Ev.Parse(buffer, EvData(i))

            '--- Guess Class Name of Actor
            actorIdx = Act.ParseActor(EvData(i).actorId, EvData(i).actorName, EvData(i).actorFlag)
            If Act.Actors(actorIdx).isRaid And Act.Actors(actorIdx).isPet = False Then
                GuessClassName(Act, actorIdx, EvData(i))
            End If

            '--- update roll guild, enemy

            '--- Zone

            '--- Difficulty




            '--- show progression
            My.Application.DoEvents()
            If CInt(i / CLF.CombatLog.Length * 100) <> prog Then
                prog = CInt(i / CLF.CombatLog.Length * 100)
                ShowProgress(prog)
            End If
        Next
    End Sub

    Private Sub GuessClassName(ByRef Act As Actors, ByVal ActorIdx As Integer, ByRef EvData As EventDB.EventDataEntry)
        If mUseArmory And (Act.Actors(ActorIdx).classIdx = WowCrawler.SpellAndClassDB.ClassIndex.Unknown Or Act.Actors(ActorIdx).classIdx = WowCrawler.SpellAndClassDB.ClassIndex.Any) Then
            Dim c As String = CharaInfo.GetCharactersInfo(EvData.actorName, mRealm, mRegion).ClassName
            Act.Actors(ActorIdx).classIdx = GuessClsName.GetClassIndex(c)
        End If

        If EvData.eventIdx = EventDB.EventIndex.DAMAGE_SHIELD Or _
            EvData.eventIdx = EventDB.EventIndex.RANGE_DAMAGE Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_ENERGIZE Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_PERIODIC_ENERGIZE Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_CAST_START Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_CAST_SUCCESS Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_CREATE Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_DAMAGE Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_PERIODIC_DAMAGE Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_DISPEL Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_EXTRA_ATTACKS Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_HEAL Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_PERIODIC_HEAL Or _
            EvData.eventIdx = EventDB.EventIndex.SPELL_INTERRUPT Then

            If EvData.spellId <> 0 Then
                Dim cidx As WowCrawler.SpellAndClassDB.ClassIndex = GuessClsName.GuessClassName(EvData.spellName, EvData.spellId, Act.Actors(ActorIdx).classIdx, mUpdateSpellDb)
                If mUseArmory = False And cidx <> WowCrawler.SpellAndClassDB.ClassIndex.Any And cidx <> WowCrawler.SpellAndClassDB.ClassIndex.Unknown Then
                    Act.Actors(ActorIdx).classIdx = cidx
                End If
            End If
        End If

    End Sub
End Class
